package com.cenntro.oss.controller;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cenntro.common.base.BaseController;
import com.cenntro.common.util.result.Result;
import com.cenntro.oss.model.dbo.DevFollowDO;
import com.cenntro.oss.model.qo.DevFollowQO;
import com.cenntro.oss.service.DevFollowService;
import com.google.common.collect.ImmutableMap;

@RestController
@RequestMapping("/follow")
public class DevFollowController extends BaseController {

	@Resource
	private DevFollowService devFollowService;

	/**
	 * 添加关注
	 * 
	 * @param qo
	 * @return
	 */
	@RequestMapping("/add")
	@CrossOrigin
	public Result add(DevFollowQO qo) {
		DevFollowDO followDO = new DevFollowDO();
		if (qo.getDevId() != null && qo.getAgentId() != null) {
			DevFollowDO devFollowDO = devFollowService.selectOne(ImmutableMap.of("devId", qo.getDevId()));
			if (devFollowDO != null) {
				return Result.ok();
			}
			followDO.setDevId(qo.getDevId());
			followDO.setAgentID(qo.getAgentId());
			followDO.setCreateTime(new Date());
			int insert = devFollowService.insert(followDO);
			if (insert > 0) {
				return Result.ok();
			} else {
				return Result.error("error");
			}
		} else {
			return Result.error("参数错误");
		}

	}

	/**
	 * 取消关注
	 * 
	 * @param devId
	 * @return
	 */
	@RequestMapping("/delete")
	@CrossOrigin
	
	public Result delete(Integer devId) {
		int delete = devFollowService.delete(devId);
		if (delete == 1) {
			return Result.ok();
		} else if(delete==0) {
			return Result.error("车辆未关注");
		}
        return null;
	}
	
}
